<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Chart extends Common
{
    public $think;

    public function __construct()
    {
        parent::__construct();
        $this->think = $_SESSION['think'];
    }
    public function index(){
        $box = input('box');
        if(request()->isAjax()){
            $type = input('post.type');
            $limit = input('post.limit')?input('post.limit'):100;
            $list =db('chart')->where(array('type'=>$type,'boxid'=>$box))->limit($limit)->order('id desc')->select();
            for($i=0; $i<$limit; $i++){
                if( !empty($list[$i]) ){
                    $createtime[] = date('H:i:s',$list[$i]['createtime']);
                    if ($type=='C'){
                        $arr[] = $list[$i]['number']*1000;
                    }else{
                        $arr[] = $list[$i]['number'];
                    }
                }
            }
            $out = array(
                'arr' => $arr,
                'createtime'   => $createtime,
            );
            echo json_encode($out);
        }else{
            $this->assign('box', $box);
            return view();
        }

    }

    public function chart_hongwai(){
        $starttime = input('starttime');
        $port = input('port');
        $time = 0;
        if(request()->isAjax()){
            if ($starttime>0){
                $temp = db('chart_hongwai')->where('createtime >'.$starttime.' AND number < 1500')->order('id asc')->find();
                $time = $temp['createtime'] - $starttime;
                if ($time<0){
                    $time = abs($time);
                    $time = date('H:i:s',substr($time,0,10)).':'.substr($time,-3);
                } else{
                    $time = str_pad($time,13,"0",STR_PAD_LEFT);
                    $time = date('H:i:s',(int)substr($time,0,10)).':'.substr($time,-3);
                }
            }
            $limit = input('post.limit')?input('post.limit'):100;
            $list =db('chart_hongwai')->where('id > 0')->limit($limit)->order('id desc')->select();
            for($i=0; $i<$limit; $i++){
                if( !empty($list[$i]) ){
                    $createtime[] = date('H:i:s',substr($list[$i]['createtime'],0,10)).':'.substr($list[$i]['createtime'],-3);
                  if ($list[$i]['port']==778){
                      $arr[$list[$i]['port']][] = $list[$i]['number']+500;

                  }else{
                      $arr[$list[$i]['port']][] = $list[$i]['number'];
                  }
                }
            }
            $out = array(
                'arr778' => $arr[778],//
                'arr779' => $arr[779],
                'createtime'   => $createtime,
                'time' =>substr($time,-9),
            );
            echo json_encode($out);
        }else{
            return view();
        }

    }
    public function chart_gls(){
        $starttime = input('starttime');
        $time = 0;
        if(request()->isAjax()){
            if ($starttime>0){
                $temp = db('chart_gls')->where('createtime >'.$starttime.' AND number < 2000')->order('id asc')->find();
                $time = $temp['createtime'] - $starttime;
                if ($time<0){
                    $time = abs($time);
                    $time = date('H:i:s',substr($time,0,10)).':'.substr($time,-3);
                } else{
                    $time  = '00000000'.$time;
                    $time = date('H:i:s',(int)substr($time,0,10)).':'.substr($time,-3);

                }
            }
            $limit = input('post.limit')?input('post.limit'):100;
            $list =db('chart_gls')->where('id > 0')->limit($limit)->order('id desc')->select();
            for($i=0; $i<$limit; $i++){
                if( !empty($list[$i]) ){
                    $createtime[] = date('H:i:s',substr($list[$i]['createtime'],0,10)).':'.substr($list[$i]['createtime'],-3);
                    $arr[] = $list[$i]['number'];
                }
            }
            $out = array(
                'arr' => $arr,
                'createtime'   => $createtime,
                'time' =>$time,
            );
            echo json_encode($out);
        }else{
            return view();
        }

    }

}
